package com.hsogoo.avatar.transport.netty;

import io.netty.channel.Channel;
import io.netty.channel.ChannelFactory;
import io.netty.channel.ServerChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @Author huangsaigang
 * Created @2018/9/29.
 */
public class SocketChannelProvider<T extends Channel> implements ChannelFactory<T> {

    public static final ChannelFactory<ServerChannel> AVATAR_ACCEPTOR = new SocketChannelProvider<>(ChannelType.ACCEPTOR);
    public static final ChannelFactory<Channel> AVATAR_CONNECTOR = new SocketChannelProvider<>(ChannelType.CONNECTOR);

    private ChannelType channelType;

    public SocketChannelProvider(ChannelType channelType) {
        this.channelType = channelType;
    }

    @Override
    public T newChannel() {
        switch (channelType) {
            case ACCEPTOR:
                return (T) new NioServerSocketChannel();
            case CONNECTOR:
                return (T) new NioSocketChannel();
            default:
                throw new IllegalStateException("Invalid channel type: " + channelType);
        }
    }

    public enum ChannelType {
        ACCEPTOR,
        CONNECTOR
    }

}
